package com.chenfubing.ps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfubing.ps.entity.Community;
import com.chenfubing.ps.entity.User;
import com.chenfubing.ps.mapper.CommunityMapper;
import com.chenfubing.ps.mapper.UserMapper;
import com.chenfubing.ps.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chenfubing
 * @since 2020-05-05
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private CommunityMapper communityMapper;

    @Override
    public void addUser(User user) {
        baseMapper.insert(user);
    }

    @Override
    public Page<User> getUserPage(int pageNum, int pageSize) {
        return baseMapper.getUserPage(new Page<>(pageNum, pageSize));
    }

    @Override
    public User findUser(Integer userId) {
        User user = baseMapper.selectById(userId);
        Community communityId = communityMapper.selectOne(new QueryWrapper<Community>().eq("community_id", user.getCommunityId()).last("limit 1"));
        user.setCommunityName(communityId.getCommunityName());
        return user;
    }

    @Override
    public User findUserByLicense(String carLicense) {
       return baseMapper.findUserByLicense(carLicense);
    }
}
